import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import pyqtSlot

@pyqtSlot(int, int)
def on_cell_clicked(row, column):
    item = table.item(row, column)
    if item is not None:
        print(f'Clicked on cell: Row {row + 1}, Col {column + 1}, Value: {item.text()}')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()

    # 创建一个 QTableWidget 实例，设置行数和列数
    table = QTableWidget(3, 4, window)

    # 设置表格的位置和大小
    table.setGeometry(10, 10, 400, 300)

    # 设置表格的列标题
    table.setHorizontalHeaderLabels(['Column 1', 'Column 2', 'Column 3', 'Column 4'])

    # 设置表格的行标题
    table.setVerticalHeaderLabels(['Row 1', 'Row 2', 'Row 3'])

    # 向表格中添加数据
    for row in range(3):
        for col in range(4):
            item = QTableWidgetItem(f'Row {row + 1}, Col {col + 1}')
            table.setItem(row, col, item)

    # 获取指定单元格的数据
    item = table.item(0, 0)
    if item is not None:
        print(item.text())

    # 连接单元格点击信号到槽函数
    table.cellClicked.connect(on_cell_clicked)

    # 显示窗口
    window.show()

    # 进入应用程序的主循环
    sys.exit(app.exec_())